package com.leyou.upload.controller;

import com.leyou.upload.service.UploadService;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图片上传到服务器上的Controller
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Logger
@CrossOrigin
@RestController
@RequestMapping("/image")
public class UploadController {

    /**
     * 注入UploadService
     */
    @Autowired
    private UploadService uploadService;

    /**
     * 将图片上传到图片服务器, 并返回URL
     * @param file 图片文件
     * @return String
     */
    @PostMapping
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file){

        String url = uploadService.upload(file);
        if(StringUtils.isBlank(url)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(url);
    }
}
